package com.gjr.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gjr.commonutils.Result;
import com.gjr.commonutils.ToolUtil;
import com.gjr.eduservice.entity.EduCourse;
import com.gjr.eduservice.entity.EduCourseDescription;
import com.gjr.eduservice.entity.EduTeacher;
import com.gjr.eduservice.entity.vo.CourseInfoVo;
import com.gjr.eduservice.entity.vo.CoursePublishVo;
import com.gjr.eduservice.entity.vo.EduCourseVo;
import com.gjr.eduservice.mapper.EduCourseMapper;
import com.gjr.eduservice.service.EduChapterService;
import com.gjr.eduservice.service.EduCourseDescriptionService;
import com.gjr.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gjr.eduservice.service.EduVideoService;
import com.gjr.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.beans.Beans;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author gjr
 * @since 2022-09-01
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    // 注入小节
    @Autowired
    private EduVideoService eduVideoService;
    // 注入章节
    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 添加课程基本信息
     *
     * @param courseInfoVo 课程对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCourseInfo(CourseInfoVo courseInfoVo) {
        // 1、向课程表添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        boolean courseSave = this.save(eduCourse);
        if (!courseSave)
            throw new GuliException(20001, "添加课程信息失败");

        // 获取添加之后课程ID
        String courseId = eduCourse.getId();

        // 2、向课程简介表添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        // 设置描述ID就是课程ID
        eduCourseDescription.setId(courseId);
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        boolean descriptionSave = eduCourseDescriptionService.save(eduCourseDescription);
        if (!descriptionSave)
            throw new GuliException(20001, "添加课程简介信息失败");

        return courseId;
    }

    /**
     * 根据课程Id 查询课程基本信息
     *
     * @param courseId 课程Id
     * @return 课程信息
     */
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        // 1、查询课程表
        EduCourse eduCourse = this.getOne(new LambdaQueryWrapper<EduCourse>()
                .eq(EduCourse::getId, courseId), false);

        // 课程表没有数据时
        if (ObjectUtils.isEmpty(eduCourse)) return null;

        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        // 2、查询描述表
        EduCourseDescription courseDescription = eduCourseDescriptionService.getOne(new LambdaQueryWrapper<EduCourseDescription>()
                .eq(EduCourseDescription::getId, courseId), false);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    /**
     * 修改课程信息
     *
     * @param courseInfoVo 课程信息对象
     * @return 成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        // 1、修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);

        boolean CourseFlag = this.updateById(eduCourse);
        if (!CourseFlag) throw new GuliException(20001, "课程表修改失败");

        // 2、修改课程描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());

        boolean descriptionFlag = eduCourseDescriptionService.updateById(eduCourseDescription);
        if (!descriptionFlag) throw new GuliException(20002, "课程描述表修改失败");
    }

    /**
     * 根据课程id查询课程确认信息
     *
     * @param courseId 课程id
     * @return 课程确认信息
     */
    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        // 调用mapper
        return baseMapper.getPublishCourseInfo(courseId);
    }

    /**
     * 课程列表
     *
     * @param eduCourseVo 分页和条件查询对象
     * @return 结果
     */
    @Override
    public HashMap<String, Object> getCourseList(EduCourseVo eduCourseVo) {
        // 创建 page 对象
        Page<EduCourse> page = new Page<>(eduCourseVo.getPage(), eduCourseVo.getLimit());

        String title = eduCourseVo.getTitle();
        String status = eduCourseVo.getStatus();

        // 根据条件查询
        LambdaQueryWrapper<EduCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(title), EduCourse::getTitle, title)
                .eq(StringUtils.hasText(status), EduCourse::getStatus, status)
                .orderByDesc(EduCourse::getGmtCreate);
        Page<EduCourse> eduCoursePage = this.page(page, wrapper);

        List<EduCourse> records = eduCoursePage.getRecords();
        if (records.isEmpty() || records.size() == 0) return null;

        // 存放查询出来的结果
        List<CoursePublishVo> returnList = new ArrayList<>();
        for (EduCourse course : records) {
            // 课程id
            String courseId = course.getId();
            CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
            returnList.add(publishCourseInfo);
        }

        return ToolUtil.returnData(eduCoursePage, returnList);
    }

    /**
     * 删除课程
     * 小节、章节、描述、课程本身都需要删除
     *
     * @param courseId 课程id
     * @return 返回值
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeCourse(String courseId) {
        // 1、根据课程id删除小节
        eduVideoService.removeVideoByCourseId(courseId);

        // 2、根据课程id删除章节
        eduChapterService.removeChapterByCourseId(courseId);

        // 3、根据课程id删除描述
        eduCourseDescriptionService.removeById(courseId);

        // 4、删除课程本身
        boolean courseRemove = this.removeById(courseId);

        if (!courseRemove) throw new GuliException(20001, "课程删除失败");
    }

}
